package com.twb.message.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.entity.twbOrganization.TwbOrganization;
import com.twb.auth.entity.twbOrganization.dto.TwbOrganizationDto;
import com.twb.auth.entity.twbUser.dto.TwbUserDto;
import com.twb.auth.service.ITwbOrganizationService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.BaseLoginUser;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import com.twb.message.constants.TwbMessageConstants;
import com.twb.message.entity.message.Message;
import com.twb.message.entity.message.dto.GetMessageByIdDto;
import com.twb.message.entity.message.dto.SearchMessageDto;
import com.twb.message.entity.message.vo.SaveMessageVo;
import com.twb.message.entity.message.vo.SearchMessageByUserVo;
import com.twb.message.entity.message.vo.SearchMessageVo;
import com.twb.message.entity.message.vo.UpdateMessageByIdVo;
import com.twb.message.service.IMessageReceivorService;
import com.twb.message.service.IMessageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title MessageController
 * @Description 系统消息管理
 * 
 * @author DHL
 * @date 2021-07-14 09:22:28
 */
@RestController
@RequestMapping("/message")
@Api(tags = "系统消息管理", value = "MessageController")
public class MessageController extends BaseController {
	@Autowired
	IMessageService messageService;

	@Autowired
	IMessageReceivorService messageReceivorService;

	@Autowired
	ITwbOrganizationService twbOrganizationService;
	/**
	 * @Title search
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@Auth(verifyToken = true)
	@GetMapping("/search")
	@ApiOperation(value = "分页查询")
	public Result<TwbPageInfo<SearchMessageDto>> search(SearchMessageVo vo) {
		TwbPageInfo<SearchMessageDto> result = messageService.searchMessage(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title getById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@Auth(verifyToken = true)
	@GetMapping("/getById")
	@ApiOperation(value = "根据ID获取详情")
	public Result<GetMessageByIdDto> getById(
			@NotBlank @RequestParam @ApiParam(value = "主键", name = "id", required = true) String id) {
		Message message = messageService.getById(id);
		if (message == null) {
			return Results.failure("数据不存在");
		}
		GetMessageByIdDto dto = new GetMessageByIdDto();
		BeanUtils.copyNotNullProperties(message, dto);
		String receivorType = message.getReceivorType();
		if(receivorType.equals(TwbMessageConstants.ReceivorType.user)) {
			List<TwbUserDto> users = messageReceivorService.listMessageReceivorUsers(id);
			dto.setUsers(users);
		}else {
			String orgIds = message.getReceivorOrgIds();
			if(StringUtils.isNotBlank(orgIds)) {
				List<TwbOrganization> list = twbOrganizationService.listByIds(Arrays.asList(orgIds.split(",")));
				try {
					List<TwbOrganizationDto> orgs = BeanUtils.copyList(TwbOrganizationDto.class, list);
					dto.setOrgs(orgs);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return Results.successWithData(dto);
	}

	/**
	 * @Title save
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@Auth(verifyToken = true)
	@PostMapping("/save")
	@ApiOperation(value = "保存信息")
	public Result<String> save(@RequestBody @Valid SaveMessageVo vo) {
		boolean result = messageService.saveMessage(vo);
		if (result) {
			return Results.success("保存成功");
		} else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title updateById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@Auth(verifyToken = true)
	@PutMapping("/updateById")
	@ApiOperation(value = "更新信息")
	public Result<String> updateById(@RequestBody @Valid UpdateMessageByIdVo vo) {
		boolean result = messageService.updateMessageById(vo);
		if (result) {
			return Results.success("更新成功");
		} else {
			return Results.failure("更新失败");
		}
	}

	/**
	 * @Title deleteById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@PostMapping("/deleteById")
	@ApiOperation(value="删除信息")
	public Result<String> deleteById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		boolean result = messageService.deleteMessageById(id);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title deleteBatchByIds
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value="批量删除信息")
	public Result<String> deleteBatchByIds(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键集合",name="id",required=true)
			String ids){
		boolean result = messageService.deleteMessageByIds(ids);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title logicDeleteById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicDeleteById")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicDeleteById(
//			@NotBlank
//			@RequestParam
//			@ApiParam(value="主键",name="id",required=true)
//			String id){
//		Message record = messageService.get(id);
//		if(null == record) {
//			return Results.failure("信息不存在");
//		}
//		record = new Message();
//		record.setId(id);
//		messageService.setUpdateInfo(record);
//		record.setIsdel(1);
//		int result = messageService.updateSelective(record);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}

	/**
	 * @Title logicBatchDeleteByIds
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DHL
	 * @date 2021-07-14 09:22:28
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicBatchDeleteByIds")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicBatchDeleteByIds(
//			@NotBlank
//			@RequestParam
//			@ApiParam(value="主键集合",name="id",required=true)
//			String ids){
//		int result = messageService.logicBatchDeleteMessageByIds(ids);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}

	/**
	 * @Title exportExcel
	 * @Description 导出excel
	 * @param exportExcelVo
	 * @param searchVo
	 * @param request
	 * @return
	 * @author DHL
	 * @throws Exception 
	 * @date 2021-07-14 09:22:28
	 */
	@Auth(verifyURL=false)
	@PostMapping("/exportExcel")
	@ApiOperation(value="导出excel")
	public void exportExcel(@RequestBody SearchMessageVo vo, HttpServletResponse response) throws Exception{
		messageService.exportExcel(vo, response);
	}

	/**
	 * @Title publish
	 * @Description 发布消息
	 * @param vo
	 * @return
	 * @author DHL
	 * @date 2021年7月14日 下午3:41:46
	 */
	@Auth(verifyToken = true)
	@PostMapping("/publish")
	@ApiOperation(value = "发布消息")
	public Result<String> publish(String messsageId) {
		Message message = messageService.getById(messsageId);

		if (message == null) {
			return Results.failure("数据不存在");
		}
		boolean result = messageService.publish(message);
		if (result) {
			return Results.success("发布消息成功");
		} else {
			return Results.failure("发布消息失败");
		}
	}

	/**
	 * @Title cancel
	 * @Description 撤销发布
	 * @param messsageId
	 * @param revokeReason
	 * @return
	 * @author DHL
	 * @date 2021年7月15日 上午9:55:55
	 */
	@Auth(verifyToken = true)
	@PostMapping("/cancel")
	@ApiOperation(value = "撤销消息")
	public Result<String> cancel(
			@NotBlank @RequestParam @ApiParam(value = "消息主键", name = "messsageId", required = true) String messsageId,

			@NotBlank @RequestParam @ApiParam(value = "撤销原因", name = "revokeReason", required = true) String revokeReason) {
		boolean result = messageService.cancel(messsageId, revokeReason);
		if (result) {
			return Results.success("撤销消息成功");
		} else {
			return Results.failure("撤销消息失败");
		}
	}

	/**
	 * @Title searchMessageByUserPage
	 * @Description 分页查询用户消息
	 * @param vo
	 * @return
	 * @author DHL
	 * @date 2021年9月7日 上午10:29:11
	 */
	@Auth(verifyToken = true, verifyURL = false)
	@GetMapping("/searchMessageByUser")
	@ApiOperation(value = "分页查询用户消息")
	public Result<TwbPageInfo<SearchMessageDto>> searchMessageByUser(SearchMessageByUserVo vo) {
		BaseLoginUser loginUser = this.getLoginUser();
		vo.setReceivorDepartmentId(loginUser.getOrgId());
		vo.setReceivorCompanyId(loginUser.getCompanyOrgId());
		TwbPageInfo<SearchMessageDto> result = messageService.searchMessageByUser(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title readMessage
	 * @Description 阅读消息
	 * @param id
	 * @return
	 * @author DHL
	 * @date 2021年7月19日 下午5:32:00
	 */
	@Auth(verifyURL = false)
	@GetMapping("/readMessage")
	@ApiOperation(value = "阅读消息")
	public Result<String> readMessage(
			@NotBlank @RequestParam @ApiParam(value = "主键", name = "id", required = true) String id) {
		Message message = messageService.getById(id);
		if (message == null) {
			return Results.failure("数据不存在");
		}
		boolean result = messageService.readMessage(message);
		if (result) {
			return Results.success("消息已阅读");
		} else {
			return Results.failure("消息阅读失败");
		}
	}
}